package com.example.demo.business.service;

import com.example.demo.base.bean.Meta;
import com.example.demo.base.exceptions.BaseException;
import com.example.demo.base.util.CommonUtil;
import com.example.demo.business.bean.entity.HOrder;
import com.example.demo.business.bean.entity.HProduct;
import com.example.demo.business.bean.entity.HUser;
import com.example.demo.business.bean.enums.OrderStatus;
import com.example.demo.business.bean.enums.UserType;
import com.example.demo.business.bean.vo.BuyVipVo;
import com.example.demo.business.dao.HOrderDao;
import com.example.demo.business.dao.HProductDao;
import com.example.demo.business.dao.HUserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class VipService {
    @Autowired
    private TokenService tokenService;

    @Autowired
    private HProductDao hProductDao;

    @Autowired
    private ProxyService proxyService;

    @Autowired
    private HUserDao hUserDao;

    @Autowired
    private HOrderDao hOrderDao;

    public Object buyVip(String token,int productId,int payChannel){
        HUser hUser = tokenService.getUserInfoByToken(token);
        BigDecimal price = getProductById(productId).getPrice();
        String orderId = createOrder(hUser.getId(),price,productId,String.valueOf(payChannel));
        //支付订单
        payOrder(orderId);
        //会员返利设置
        proxyService.calProxyReward(hUser,productId,price.intValue());
        //设置会员
        setVip(hUser,productId);
        //TODO 购买之后下载次数增加

        return new BuyVipVo("https://3gqq.qq.com?orderId="+orderId);
    }

    public Object getAllProduct(){
        return hProductDao.findAll();
    }


    private void setVip(HUser hUser,int type){
        HProduct hProduct = hProductDao.findById(type).get();
        long time = 0L;
        switch (hProduct.getPayType()){
            case 1:
                //月卡
                time = 60*60*24L*30L;

                break;
            case 2:
                //半年卡
                time = 60*60*24*30L*6L;

                break;
            case 3:
                //年卡
                time = 60*60*24*30L*12L;

                break;
        }
        long currentTime = System.currentTimeMillis()/1000;
        if (currentTime >= hUser.getVipDueTime()){
            //如果VIP到期了
            hUser.setVipDueTime(currentTime+time);
        }else if (currentTime < hUser.getVipDueTime()){
            //如果VIP没有到期
            hUser.setVipDueTime(hUser.getVipDueTime()+time);
        }
        hUser.setAllDown(10);
        hUser.setRemainDown(10);
        hUser.setUserType(UserType.Vip.num);
        hUser.setVipType(hUser.getVipType()>hProduct.getPayType()?hUser.getVipType():hProduct.getPayType());
        tokenService.updateUserInfo(hUser);
    }

    public HProduct getProductById(int id){
        HProduct hProduct = hProductDao.findById(id).orElseThrow(()->new BaseException(Meta.ServerError));
        return hProduct;
    }
    public HProduct getProductByType(int type){
        if (type == 1 || type==2 ||type ==3){
            return hProductDao.findByPayType(type);
        }else {
            HProduct hProduct = new HProduct();
            hProduct.setDescribes("每天免费看一部电影==无法下载");
            hProduct.setPayType(0);
            return hProduct;
        }

    }
    //创建订单
    private String createOrder(int uid,BigDecimal price,int type,String payChannel){
        HOrder hOrder = new HOrder(uid,getOrderId(uid),price,type, OrderStatus.WaitPay.num,payChannel,System.currentTimeMillis()/1000);
        hOrderDao.save(hOrder);
        return hOrder.getOrderId();
    }
    //支付订单
    private void payOrder(String orderId){
        HOrder hOrder = hOrderDao.findByOrderId(orderId);
        hOrder.setPayTime(System.currentTimeMillis());
        hOrder.setStatus(OrderStatus.Paid.num);
        hOrderDao.save(hOrder);
    }

    private String getOrderId(int uid){
        uid = uid+100000;
        int uunum = 100000+ CommonUtil.getRandomCount(99999);
        return String.valueOf(System.currentTimeMillis())+uid+""+uunum;
    }


}
